package com.plume.toolbox.plus;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;

import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;

import com.plume.toolbox.plus.databinding.ActivityMainBinding;
import com.hnphqs.tools;
import com.plume.toolbox.plus.jni.PlumeNative;
import com.plume.toolbox.plus.ui.DisclaimerDialogFragment;
import java.util.HashMap;
import java.util.Map;


public class MainActivity extends AppCompatActivity {
    private String tip_1="你逆向干什么?这软件是开源的!";
    private String tip_2="诺，源代码仓库在tip_3";
    private String tip_3="https://gitee.com/hnphqs/plume-toolbox";
    private String tip_4="看之前别忘了给我个star啊";
    private AppBarConfiguration mAppBarConfiguration;
    private ActivityMainBinding binding;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
       // 检查是否已经同意免责声明
        SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
        boolean agreedToDisclaimer = prefs.getBoolean("agreed_to_disclaimer", false);

        if (!agreedToDisclaimer) {
            // 展示免责声明
            DisclaimerDialogFragment dialogFragment = new DisclaimerDialogFragment();
            dialogFragment.show(getSupportFragmentManager(), "disclaimer_dialog");
        }
        
        UserStat.isVip(this);
        
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setSupportActionBar(binding.appBarMain.toolbar);
        
        
        DrawerLayout drawer = binding.drawerLayout;
        NavigationView navigationView = binding.navView;
        
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
                .setOpenableLayout(drawer)
                .build();
        
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
        
        String title = "Plume工具箱";
        String message = "v0.1 beta\n欢迎使用Plume工具箱，本应用集成编程猫实用功能，欢迎入群讨论";
        String QQkey = "E6pIZEjqXvJe_20Wln0M99f7EFhbm1uL";
        showCustomAlertDialog(title, message, QQkey);
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        menu.add(0,0,0,"关于");
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        
        switch(item.getItemId()){
            case 0:
                showAboutAlertDialog();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
        
    } 
    
    public void showAboutAlertDialog(){
       AlertDialog.Builder builder = new AlertDialog.Builder(this);
       builder.setTitle("关于")
           .setMessage(R.string.about)
           .setPositiveButton("确定", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   dialog.dismiss();
                   
               }
           })
           .setCancelable(false) // 不允许按返回键取消对话框
           .create()
           .show();
    }
    
    public void showCustomAlertDialog(String title, String message, String QQkey) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(title)
           .setMessage(message)
           .setPositiveButton("确定", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   dialog.dismiss();
                   
               }
           })
           .setNegativeButton("加入QQ群", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
                   tools.openQqGroup(MainActivity.this,QQkey);
               }
           })
           .setCancelable(false) // 不允许按返回键取消对话框
           .create()
           .show();
}
    
    @Override
    public String toString() {
        return "MainActivity[binding=" + binding + "]";
    }
}